package com.example.demo.controller;

import com.example.demo.custom.CustomResponse;
import com.example.demo.model.User;
import com.example.demo.service.IuserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@AllArgsConstructor
public class UserController {

    private final IuserService iuserService;

    // 创建用户
    @PostMapping("/create")
    public CustomResponse createUser(@RequestBody User user) {
        iuserService.insertUser(user);
        return new CustomResponse().success();
    }

    // 获取所有用户
    @GetMapping("/getAllUsers")
    public PageInfo<User> getAllUsers(@RequestParam int page, @RequestParam int size) {
        PageHelper.startPage(page, size);
        List<User> users = iuserService.getAllUsers();
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return pageInfo;
    }

    // 创建用户
    @GetMapping("/getUserInfo/{userId}")
    public CustomResponse getUserInfo(@PathVariable("userId") Long userId) {
        return new CustomResponse("ok", iuserService.getUserInfo(userId));
    }

    // 根据 ID 获取用户
    @GetMapping("/users/{id}")
    public User getUserById(@PathVariable Long id) {
        return iuserService.getUserById(id);
    }

    // 更新用户信息
    @PutMapping("/users/{id}")
    public void updateUser(@PathVariable Long id, @RequestBody User userDetails) {
        userDetails.setId(id);
        iuserService.updateUser(userDetails);
    }

    // 根据 ID 删除用户
    @DeleteMapping("/users/{id}")
    public void deleteUser(@PathVariable Long id) {
        iuserService.deleteUser(id);
    }
}